package com.macaque.example.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.macaque.base.controller.BaseController;
import com.macaque.example.model.User;
import com.macaque.example.service.UserService;

@Controller("Usercontrollere")
@RequestMapping("/example/user")
public class Usercontroller extends BaseController {
	@Autowired
	private UserService userService;
	@RequestMapping(value ="/list",method = RequestMethod.GET)
	public String getList(HttpServletRequest request,ModelMap modelMap) {
		List<User> userList = userService.getList();
		modelMap.put("userList", userList);
		request.setAttribute("c", "c");
		modelMap.put("d", "d");
		return "example/list";
	}

	@RequestMapping("/{id}/show")
	public String showUser(@PathVariable String id, HttpServletRequest request) {
		User u = userService.getUserById(id);
		request.setAttribute("user", u);
		return "example/detail";
	}
	
	
	@ResponseBody
	@RequestMapping("/listJson")
	public List<User> getListJson(HttpServletRequest request) {
		List<User> userList = userService.getList();
		return userList;
	}
	
	
	@RequestMapping(value ="/{id}/delete",method = RequestMethod.GET)
	public String delete(@PathVariable String id,HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) {
		userService.deleteById(id);
		request.setAttribute("a", "a");
		modelMap.put("b", "b");
		//return getList(request);
		return "redirect:/example/user/list";
	}
	
	@RequestMapping("/add")
	public String add(User user,HttpServletRequest request,ModelMap modelMap) {
		modelMap.put("optype", "add");
		return "example/edit";
	}
	@RequestMapping("{id}/edit")
	public String edit(@PathVariable String id,HttpServletRequest request,ModelMap modelMap) {
		modelMap.put("optype", "edit");
		User u = userService.getUserById(id);
		request.setAttribute("user", u);
		return "example/edit";
	}
	@RequestMapping(value = "/addSave",method = RequestMethod.POST)
	public String addSave(User user,HttpServletRequest request) {
		user.setId(UUID.randomUUID().toString());
		userService.save(user);
		//List<User> userList = userService.getUserList();
		//request.setAttribute("userList", userList);
		//return "forward:list";
		return "redirect:list";
	}
	
	
	/**
	 * <p>Discription:[方法功能中文描述]</p>
	 * @param file
	 * @param request
	 * @return
	 * @author:[创建者中文名字]
	 * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@ResponseBody
	@RequestMapping(value = "/upload")
	public String uploadImg(@RequestParam("file") MultipartFile file,
			HttpServletRequest request) {
		System.out.println(request.getSession().getServletContext().getRealPath("/"));
		String uploadPath = request.getSession().getServletContext().getRealPath("/")+"upload";
		System.out.println(uploadPath+File.separator+file.getOriginalFilename());
		if (null != file) {
			System.out.println(file.getOriginalFilename());
			File newFile = new File(uploadPath+File.separator+file.getOriginalFilename());
			try {
				file.transferTo(newFile);
			} catch (IOException e) {
				e.printStackTrace(); // To change body of catch statement use
										// File | Settings | File Templates.
			}
		}
		//return "redirect:list";
		return "hahahaha";
	}
}
